package cn.study.executorService;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author     ：跟大佳一起学架构
 * @Description：抛出异常后，为啥要销毁当前线程在重新创建... ...
 */
public class ThreadPoolErrorDemo {
    public static void main(String[] args) throws InterruptedException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 1000, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(200),
        new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread thread = new Thread(r);
                // 因为
                thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
                    @Override
                    public void uncaughtException(Thread t, Throwable e) {
                        System.out.println("出错拉...");
                    }
                });
                return thread;
            }
        });

        executor.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println("...");
                throw new NullPointerException();
            }
        });

        Thread.sleep(100);
        System.out.println(executor.isTerminated());
    }
}
